<?php

require_once __DIR__ . '/vendor/autoload.php';

$app = require_once __DIR__ . '/bootstrap/app.php';
$app->make('Illuminate\Contracts\Console\Kernel')->bootstrap();

try {
    echo "🚀 开始创建完整的基准数据...\n";

    // 1. 创建管理员角色
    $adminRoleId = \DB::table('roles')->insertGetId([
        'name' => '系统管理员',
        'code' => 'admin',
        'description' => '拥有系统所有权限的超级管理员角色',
        'type' => 'system',
        'status' => 'active',
        'permissions' => json_encode([
            'system.manage',
            'system.settings',
            'system.backup',
            'system.logs',
            'user.manage',
            'user.view',
            'user.create',
            'user.edit',
            'user.delete',
            'role.manage',
            'role.view',
            'role.create',
            'role.edit',
            'role.delete',
            'role.assign',
            'hr.manage',
            'employee.manage',
            'employee.view',
            'employee.create',
            'employee.edit',
            'employee.delete',
            'department.manage',
            'department.view',
            'department.create',
            'department.edit',
            'department.delete',
            'position.manage',
            'position.view',
            'position.create',
            'position.edit',
            'position.delete'
        ]),
        'created_at' => now(),
        'updated_at' => now(),
    ]);
    echo "✅ 创建系统管理员角色 ID: $adminRoleId\n";

    // 2. 创建普通用户角色
    $userRoleId = \DB::table('roles')->insertGetId([
        'name' => '普通用户',
        'code' => 'user',
        'description' => '普通用户角色，具有基本的查看权限',
        'type' => 'custom',
        'status' => 'active',
        'permissions' => json_encode([
            'user.view',
            'hr.manage',
            'employee.view',
            'department.view',
            'position.view'
        ]),
        'created_at' => now(),
        'updated_at' => now(),
    ]);
    echo "✅ 创建普通用户角色 ID: $userRoleId\n";

    // 3. 创建菜单数据
    $menus = [
        [
            'parent_id' => null,
            'name' => '仪表板',
            'code' => 'dashboard',
            'icon' => 'dashboard',
            'path' => '/dashboard',
            'component' => 'Dashboard',
            'type' => 'menu',
            'description' => '系统仪表板，显示关键统计信息',
            'permissions' => json_encode([]),
            'status' => 'active',
            'is_external' => false,
            'is_visible' => true,
            'sort_order' => 1,
            'created_at' => now(),
            'updated_at' => now(),
        ],
        [
            'parent_id' => null,
            'name' => '系统管理',
            'code' => 'system',
            'icon' => 'setting',
            'path' => '/system',
            'component' => null,
            'type' => 'menu',
            'description' => '系统设置和管理功能',
            'permissions' => json_encode(['system.manage']),
            'status' => 'active',
            'is_external' => false,
            'is_visible' => true,
            'sort_order' => 2,
            'created_at' => now(),
            'updated_at' => now(),
        ],
        [
            'parent_id' => null,
            'name' => '人事管理',
            'code' => 'hr',
            'icon' => 'team',
            'path' => '/hr',
            'component' => null,
            'type' => 'menu',
            'description' => '人事档案和组织管理',
            'permissions' => json_encode(['hr.manage']),
            'status' => 'active',
            'is_external' => false,
            'is_visible' => true,
            'sort_order' => 3,
            'created_at' => now(),
            'updated_at' => now(),
        ],
    ];

    $menuIds = [];
    foreach ($menus as $menu) {
        $id = \DB::table('menus')->insertGetId($menu);
        $menuIds[$menu['code']] = $id;
        echo "✅ 创建菜单: {$menu['name']} (ID: $id)\n";
    }

    // 4. 创建子菜单
    $subMenus = [
        // 系统管理子菜单
        [
            'parent_id' => $menuIds['system'],
            'name' => '用户管理',
            'code' => 'system.users',
            'icon' => 'user',
            'path' => '/system/users',
            'component' => 'system/Users',
            'type' => 'menu',
            'description' => '管理系统用户账户',
            'permissions' => json_encode(['user.manage']),
            'status' => 'active',
            'is_external' => false,
            'is_visible' => true,
            'sort_order' => 1,
            'created_at' => now(),
            'updated_at' => now(),
        ],
        [
            'parent_id' => $menuIds['system'],
            'name' => '角色管理',
            'code' => 'system.roles',
            'icon' => 'usergroup-add',
            'path' => '/system/roles',
            'component' => 'system/Roles',
            'type' => 'menu',
            'description' => '管理系统角色和权限',
            'permissions' => json_encode(['role.manage']),
            'status' => 'active',
            'is_external' => false,
            'is_visible' => true,
            'sort_order' => 2,
            'created_at' => now(),
            'updated_at' => now(),
        ],
        [
            'parent_id' => $menuIds['system'],
            'name' => '菜单管理',
            'code' => 'system.menus',
            'icon' => 'menu',
            'path' => '/system/menus',
            'component' => 'system/Menus',
            'type' => 'menu',
            'description' => '管理系统菜单结构',
            'permissions' => json_encode(['system.manage']),
            'status' => 'active',
            'is_external' => false,
            'is_visible' => true,
            'sort_order' => 3,
            'created_at' => now(),
            'updated_at' => now(),
        ],
        // 人事管理子菜单
        [
            'parent_id' => $menuIds['hr'],
            'name' => '员工管理',
            'code' => 'hr.employees',
            'icon' => 'user',
            'path' => '/hr/employees',
            'component' => 'hr/Employees',
            'type' => 'menu',
            'description' => '管理员工基本信息',
            'permissions' => json_encode(['employee.manage']),
            'status' => 'active',
            'is_external' => false,
            'is_visible' => true,
            'sort_order' => 1,
            'created_at' => now(),
            'updated_at' => now(),
        ],
        [
            'parent_id' => $menuIds['hr'],
            'name' => '部门管理',
            'code' => 'hr.departments',
            'icon' => 'apartment',
            'path' => '/hr/departments',
            'component' => 'hr/Departments',
            'type' => 'menu',
            'description' => '管理组织部门结构',
            'permissions' => json_encode(['department.manage']),
            'status' => 'active',
            'is_external' => false,
            'is_visible' => true,
            'sort_order' => 2,
            'created_at' => now(),
            'updated_at' => now(),
        ],
        [
            'parent_id' => $menuIds['hr'],
            'name' => '岗位管理',
            'code' => 'hr.positions',
            'icon' => 'solution',
            'path' => '/hr/positions',
            'component' => 'hr/Positions',
            'type' => 'menu',
            'description' => '管理岗位和职位信息',
            'permissions' => json_encode(['position.manage']),
            'status' => 'active',
            'is_external' => false,
            'is_visible' => true,
            'sort_order' => 3,
            'created_at' => now(),
            'updated_at' => now(),
        ],
    ];

    foreach ($subMenus as $menu) {
        $id = \DB::table('menus')->insertGetId($menu);
        echo "✅ 创建子菜单: {$menu['name']} (ID: $id)\n";
    }

    // 5. 创建测试用户并分配角色
    $testUsers = [
        [
            'username' => 'admin',
            'phone' => '13800000000',
            'password' => \Illuminate\Support\Facades\Hash::make('123456'),
            'status' => 'active',
            'created_at' => now(),
            'updated_at' => now(),
        ],
        [
            'username' => 'user1',
            'phone' => '13800000001',
            'password' => \Illuminate\Support\Facades\Hash::make('123456'),
            'status' => 'active',
            'created_at' => now(),
            'updated_at' => now(),
        ],
    ];

    $userIds = [];
    foreach ($testUsers as $userData) {
        $userId = \DB::table('users')->insertGetId($userData);
        $userIds[$userData['username']] = $userId;
        echo "✅ 创建用户: {$userData['username']} (ID: $userId)\n";
    }

    // 6. 分配角色给用户（修复user_roles表问题）
    $userRoleAssignments = [
        [
            'user_id' => $userIds['admin'],
            'role_id' => $adminRoleId,
            'start_date' => now()->format('Y-m-d'),
            'status' => 'active',
            'reason' => '系统初始化分配',
            'created_at' => now(),
            'updated_at' => now(),
        ],
        [
            'user_id' => $userIds['user1'],
            'role_id' => $userRoleId,
            'start_date' => now()->format('Y-m-d'),
            'status' => 'active',
            'reason' => '系统初始化分配',
            'created_at' => now(),
            'updated_at' => now(),
        ],
    ];

    foreach ($userRoleAssignments as $assignment) {
        \DB::table('user_roles')->insert($assignment);
        echo "✅ 为用户 {$assignment['user_id']} 分配角色 {$assignment['role_id']}\n";
    }

    echo "🎉 完整的基准数据创建完成！\n";
    echo "\n📋 测试账户：\n";
    echo "管理员：admin / 123456\n";
    echo "普通用户：user1 / 123456\n";

} catch (Exception $e) {
    echo "❌ 错误：" . $e->getMessage() . "\n";
    echo "📍 文件：" . $e->getFile() . " 行：" . $e->getLine() . "\n";
}
